package com.android.phoenix.util;

import java.util.ArrayList;
import java.util.List;

import com.android.phoenix.bean.ActivityInfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseUtil extends SQLiteOpenHelper{

	private final static int VERSION = 1;
	private final static String DATA_BASE_NAME = "bicycle_db";
	private final static String ACTIVITY_TABLE_NAME = "activity_table";
	
	public DataBaseUtil(Context context) {
		super(context, DATA_BASE_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("drop table if exists activity_table");
		String createActivityTable = "create table if not exists " + ACTIVITY_TABLE_NAME +
				"(id integer primary key autoincrement," +
				"start_date varchar," +
				"start_time varchar," +
				"activity_time varchar," +
				"start_address varchar," +
				"end_address varchar," +
				"activity_type varchar," +
				"activity_contact varchar," +
				"contact_number varchar," +
				"activity_desc varchar)";
		db.execSQL(createActivityTable);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exists activity_table");
		String createActivityTable = "create table if not exists " + ACTIVITY_TABLE_NAME +
				"(id integer primary key autoincrement," +
				"start_date varchar," +
				"start_time varchar," +
				"activity_time varchar," +
				"start_address varchar," +
				"end_address varchar," +
				"activity_type varchar," +
				"activity_contact varchar," +
				"contact_number varchar," +
				"activity_desc varchar)";
		db.execSQL(createActivityTable);
	}
	
	public void addActivity(ActivityInfo info){
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("start_date", info.startDate);
		values.put("start_time", info.startTime);
		values.put("activity_time", info.activityTime);
		values.put("start_address", info.startAddress);
		values.put("end_address", info.endAddress);
		values.put("activity_type", info.activtyType);
		values.put("activity_contact", info.activityContact);
		values.put("contact_number", info.contactNumber);
		values.put("activity_desc", info.activityDesc);
		db.insert(ACTIVITY_TABLE_NAME, null, values);
		db.close();
	}
	
	public List<ActivityInfo> getAllActivityInfo(){
		List<ActivityInfo> mList = new ArrayList<ActivityInfo>();
		SQLiteDatabase db = getReadableDatabase();
		Cursor c = db.query(ACTIVITY_TABLE_NAME, null, null, null, null, null, null);
		while(c.moveToNext()) {
			String startDate = c.getString(c.getColumnIndex("start_date"));
			String startTime = c.getString(c.getColumnIndex("start_time"));
			String activityTime = c.getString(c.getColumnIndex("activity_time"));
			String startAddress = c.getString(c.getColumnIndex("start_address"));
			String endAddress = c.getString(c.getColumnIndex("end_address"));
			String activtyType = c.getString(c.getColumnIndex("activity_type"));
			String activityContact = c.getString(c.getColumnIndex("activity_contact"));
			String contactNumber = c.getString(c.getColumnIndex("contact_number"));
			String activityDesc = c.getString(c.getColumnIndex("activity_desc"));
			ActivityInfo info = new ActivityInfo(startDate, startTime, activityTime, startAddress, endAddress, 
					activtyType, activityContact, contactNumber, activityDesc);
			mList.add(info);
		}
		c.close();
		db.close();
		return mList;
	}
	
	

}
